package cn.bsuc.crm.service.impl;

import cn.bsuc.crm.entity.Permission;
import cn.bsuc.crm.mapper.PermissionMapper;
import cn.bsuc.crm.service.PermissionService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zhuoyuan
 * @since 2022-10-03
 */
@Service
public class PermissionServiceImp extends ServiceImpl<PermissionMapper, Permission> implements PermissionService {

    @Override
    @Transactional
    public int savePermission(Integer roleId, Integer[] moduleIds) {
        QueryWrapper<Permission> moduleQueryWrapper = new QueryWrapper<>();
        moduleQueryWrapper.eq("role_id",roleId);
        int delete = baseMapper.delete(moduleQueryWrapper);
        int i1 = 0;
        if (moduleIds.length!=0){
            int i = baseMapper.insertModuleIdByRoleId(roleId, moduleIds);
            i1 = i;
        }
        if (delete>=0&&i1>=0){
            return 1;
        }
        return 0;
    }
}
